home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Report Writers / Crystal Repot 9.0 Full CD version / Setup.exe / SRC / HOARDDLL.ZIP / 3rdParty / hoard / libhoard-2.0.2 / log2.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-01  |  2.4 KB  |  117 lines

  1. #ifndef _LOG2_H_
  2. #define _LOG2_H_
  3.  
  4. #include <assert.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <pthread.h>
  10.  
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <sys/mman.h>
  14.  
  15. template <class TYPE>
  16. class Log2 {
  17. public:
  18.  
  19.   Log2 (void)
  20.     : _start (NULL),
  21.       _actualLength (0),
  22.       _perceivedLength (0),
  23.       _isOpen (0) {}
  24.  
  25.   ~Log2 (void) {}
  26.  
  27.   enum { READ_WRITE = 0,
  28.      READ_ONLY = 1 };
  29.  
  30.   void open (const char * filename,
  31.          int readOnly = READ_ONLY)
  32.   {
  33.     if (!isOpen()) {
  34.  
  35.       // Save the filename.
  36.       strncpy ((char *) _filename, (char *) filename, 255);
  37.       
  38.       int nobjs;
  39.       
  40.       // Open the file.
  41.       // The file MUST exist and be non-empty.
  42.       _fd = ::open (_filename, O_RDONLY, 00600);
  43.       assert (_fd != -1);
  44.       
  45.       // Find out how long the file is.
  46.       struct stat buf;
  47.       fstat (_fd, &buf);
  48.       
  49.       // Calculate how many objects are in the file.
  50.       nobjs = numberOfObjects((int) buf.st_size);
  51.       assert (fileLength(nobjs) == (int) buf.st_size);
  52.  
  53.       _actualLength = nobjs;
  54.  
  55.       lseek(_fd, 0, SEEK_SET);
  56.       read (_fd, (void *) &_perceivedLength, sizeof(int));
  57.     }
  58.   }
  59.  
  60.  
  61.     // Close a log.
  62.   inline void close (void) {
  63.     ::close (_fd);
  64.   }
  65.  
  66.  
  67.   // Abort a log. (Don't write any changes.)
  68.   inline void abort (void) {
  69.     ::close (_fd);
  70.   }
  71.  
  72.   // Access a log entry directly.
  73.   inline TYPE& operator[] (int i) {
  74.     TYPE t;
  75.     lseek (_fd, i * sizeof(TYPE) + sizeof(int), SEEK_SET);
  76.     read (_fd, (void *) &t, sizeof(TYPE));
  77.     return t;
  78.   }
  79.  
  80.   // Return the number of elements in the log.
  81.   int length (void) {
  82.     assert (_isOpen);
  83.     int len = _perceivedLength;
  84.     return len;
  85.   }
  86.  
  87. private:
  88.  
  89.   int isOpen (void) {
  90.     return _isOpen;
  91.   }
  92.  
  93.   int isReadOnly (void) {
  94.     return (_readOnlyMode == READ_ONLY);
  95.   }
  96.  
  97.   // Given the total number of objects, return the file length.
  98.   int fileLength (int nobjs) {
  99.     return nobjs * sizeof(TYPE) + sizeof(int);
  100.   }
  101.  
  102.   // Given the length of a file, return the total number of objects.
  103.   int numberOfObjects (int fileLen) {
  104.     return ((int) fileLen - sizeof(int)) / sizeof(TYPE);
  105.   }
  106.  
  107.   char _filename[255];    // The log's filename.
  108.   char * _start;    // The start address of the mmapped memory block.
  109.   int _fd;
  110.   int _isOpen;
  111.   int _perceivedLength;
  112.   int _actualLength;
  113.  
  114. };
  115.  
  116. #endif // _LOG2_H_
  117.